package com.vercon.commonmirror.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import com.vercon.commonmirror.R
import com.vercon.commonmirror.utils.LogUtils

import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by YZC on 2019/9/3
 */
class DateSelect : LinearLayout {

    val TAG = javaClass.simpleName

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        bindView()
    }

    lateinit var iv_pre: ImageView
    lateinit var tv_pre: NormalTextView
    lateinit var tv_date: NormalTextView
    lateinit var tv_next: NormalTextView
    lateinit var iv_next: ImageView

    var listener: ((Pair<String, String>) -> Unit)? = null

    var calendar = Calendar.getInstance()
    val format = SimpleDateFormat("yyyy-MM-dd")
    val compareformat = SimpleDateFormat("yyyy-MM")
    var year = calendar.get(Calendar.YEAR)
    var month = calendar.get(Calendar.MONTH) + 1
    var currentDate = compareformat.parse(compareformat.format(calendar.time))
    var months = context.resources.getStringArray(R.array.months)//arrayOf("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    fun bindView() {
        LayoutInflater.from(context).inflate(R.layout.widget_smarthome_date_select, this)
        iv_pre = findViewById(R.id.iv_pre)
        tv_pre = findViewById(R.id.tv_pre)
        tv_date = findViewById(R.id.tv_date)
        tv_next = findViewById(R.id.tv_next)
        iv_next = findViewById(R.id.iv_next)

        iv_pre.setOnClickListener { onPre() }
        tv_pre.setOnClickListener { onPre() }

        tv_next.setOnClickListener { onNext() }
        iv_next.setOnClickListener { onNext() }
        // tv_date.text = "${year}年${month}月"
        tv_date.text = "${year}-${months[month-1]} "//${month}
        if (compareformat.format(calendar.time) == compareformat.format(currentDate) ){
            tv_next.setTextAppearance(context,R.style.text_color_A8AAB8)
            iv_next.setImageResource(R.mipmap.ic_arrow_right_gray)
        }
        LogUtils.d(TAG, "year $year month $month")
    }


    fun onPre() {
        calendar.add(Calendar.MONTH, -1)
        tv_next.setTextColor(Color.WHITE)
        iv_next.setImageResource(R.mipmap.ic_arrow_right_white)
        change(1)
    }

    fun onNext() {
        calendar.add(Calendar.MONTH, 1)
        if (compareformat.format(calendar.time) == compareformat.format(currentDate) ){
            tv_next.setTextAppearance(context,R.style.text_color_A8AAB8)
            iv_next.setImageResource(R.mipmap.ic_arrow_right_gray)
        }
        if (compareformat.parse(compareformat.format(calendar.time)).time - currentDate.time > 0) {
            calendar.add(Calendar.MONTH, -1)
            tv_next.setTextAppearance(context,R.style.text_color_A8AAB8)
            iv_next.setImageResource(R.mipmap.ic_arrow_right_gray)
            return
        }
        change(2)
    }

    fun change(state:Int) {
        year = calendar.get(Calendar.YEAR)
        month = calendar.get(Calendar.MONTH) + 1
        tv_date.text = "${year}-${months[month-1]} "//${month}
        // tv_date.text = "${months[month-1]} ${month},${year}"
        LogUtils.d(TAG, "year $year month $month")
        var  data: Pair<String, String>
        data = getTimeParams(state,calendar)
        listener?.invoke(data)
    }

    fun getTime():String{
        return tv_date.text.toString()
    }

    fun getTimeParams(state:Int,ca: Calendar): Pair<String, String> {
        ca.set(Calendar.DAY_OF_MONTH, 1)
        val startTime = format.format(calendar.time)
        ca.add(Calendar.MONTH, 1)
        ca.set(Calendar.DAY_OF_MONTH, 0)
        val endTime = format.format(calendar.time)
        if (onItemDoubleClick != null){
            onItemDoubleClick!!.scrollToPosition(date2TimeStamp(startTime),date2TimeStamp(endTime),state)
        }
        return Pair(date2TimeStamp(startTime), date2TimeStamp(endTime))
    }
    fun date2TimeStamp(date: String): String {
        try {
            val sdf = SimpleDateFormat("yyyy-MM-dd")
            return (sdf.parse(date).time / 1000).toString()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return ""
    }
    private var onItemDoubleClick: OnItemDoubleClick? = null
    fun setOnItemDoubleClick(onItemDoubleClick: OnItemDoubleClick) {
        this.onItemDoubleClick = onItemDoubleClick
    }

    interface OnItemDoubleClick {
        fun scrollToPosition(startTime: String,endTime: String,state:Int)
    }
}